function result = testBayNet(bnet, data, nsamples)
    N=size(bnet.dag,1);
    samples = cell(N, nsamples);
    for i=1:nsamples
        samples(:,i) = sample_bnet(bnet);
    end

    testdata = cell2num(samples);
    % Make a tabula rasa
    bnet2 = mk_bnet(bnet.dag, bnet.node_sizes);
    seed = 0;
    rand('state', seed);
    for(i=1:size(bnet.dag,1))
        bnet2.CPD{i} = tabular_CPD(bnet2, i);
    end;

    bnet2 = learn_params(bnet2, samples);

    targetCPT = cell(1,N);
    sourceCPT = cell(1,N);
    result=0;
    for i=1:N
        t=struct(bnet2.CPD{i});  % violate object privacy
        targetCPT{i}=t.CPT;
        s=struct(bnet.CPD{i});
        sourceCPT{i}=s.CPT;
        diff=abs(s.CPT-t.CPT);
        result=result+(mean(diff(:)));
    end
    result=result/N;
end